//
//  PGPhotographicViewController.m
//  PhotographicGuide
//
//  Created by indream on 13-2-10.
//  Copyright (c) 2013年 indream. All rights reserved.
//

#import "PGPhotographicViewController.h"
#import "PGWebViewController.h"
#import "SBJson.h"
#import "PGArticleManager.h"
#import "MobClick.h"
@interface PGPhotographicViewController ()

@end

@implementation PGPhotographicViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    PGArticle *article = [[PGArticleManager sharedManager]articleWithID:_articleId];
    NSString *json = article.devices;
    SBJsonParser *parser = [[SBJsonParser alloc]init];
    NSDictionary*dict = [parser objectWithString:json];
    
    self.tableView.hidden = YES;
    if(dict){
        self.groupsData = [dict objectForKey:@"group"];
        if(self.groupsData){
            self.tableView.hidden = NO;
        }
    }
    UISwipeGestureRecognizer *swiper = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeHandle:)];
    [self.view addGestureRecognizer:swiper];
    // Do any additional setup after loading the view from its nib.
}
-(void)swipeHandle:(UISwipeGestureRecognizer*)gesture{
    if(gesture.direction==UISwipeGestureRecognizerDirectionRight){
        [self backHandle:nil];
    }
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [MobClick beginLogPageView:[NSString stringWithFormat:@"Devices=%d",_articleId]];
}

-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [MobClick endLogPageView:[NSString stringWithFormat:@"Devices=%d",_articleId]];
}
- (void)viewDidUnload {
    [self setTableView:nil];
    [self setTvCell:nil];
    [super viewDidUnload];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [[[_groupsData objectAtIndex:section] objectForKey:@"items"] count];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return _groupsData.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return [[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone?44.0:88.0;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return [[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone?20.0:40.0;
}
-(NSString*)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    return [[_groupsData objectAtIndex:section] objectForKey:@"title"];
}
-(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *groupHeaderView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, [[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone?320:768, [[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone?20:40)];
    UIImageView *headerBgImage = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"header_bg@2x.jpg"]];
    [headerBgImage setFrame:groupHeaderView.frame];
    [groupHeaderView addSubview:headerBgImage];
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, [[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone?0:10, tableView.bounds.size.width-20, 20)];
    label.text = [tableView.dataSource tableView:tableView titleForHeaderInSection:section];
    label.backgroundColor = [UIColor clearColor];
    label.textColor = [UIColor blackColor];
    label.font = [UIFont boldSystemFontOfSize:[[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone?12:18];
    [groupHeaderView addSubview:label];
    return groupHeaderView;
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    PGDeviceItemCell *cell = [tableView dequeueReusableCellWithIdentifier:@"PGDeviceItemCell"];
    if (cell == nil) {
        if([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone){
            [[NSBundle mainBundle] loadNibNamed:@"PGDeviceItemCell" owner:self options:nil];
        }else{
            [[NSBundle mainBundle] loadNibNamed:@"PGDeviceItemCell_iPad" owner:self options:nil];
        }
        cell = _tvCell;
        self.tvCell = nil;
    }
    cell.titleLabel.text = [[[[_groupsData objectAtIndex:indexPath.section] objectForKey:@"items"] objectAtIndex:indexPath.row] objectForKey:@"title"];
    cell.isHiddenShop = ![[[[_groupsData objectAtIndex:indexPath.section] objectForKey:@"items"] objectAtIndex:indexPath.row] objectForKey:@"url"];
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString*url = [[[[_groupsData objectAtIndex:indexPath.section] objectForKey:@"items"] objectAtIndex:indexPath.row] objectForKey:@"url"];
    if(url){
        PGWebViewController *controller = [[PGWebViewController alloc]init];
        controller.url = url;
        controller.title = @"查看详情";
        [self.navigationController pushViewController:controller animated:YES];
    }
}

- (IBAction)backHandle:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}

- (IBAction)findHandle:(id)sender {
    [self addScreenFadin];
}
@end
